package com.alibaba.otter.node.etl.load.loader.mq.rabbit.strategy;

import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EndStrategyBuild {

    private Class<? extends Exception> currentExClass;
    private List<RescueAction> currentActions = Lists.newArrayList();
    private EndAction currentEndAction;

    private Map<Class<? extends Throwable>, List<RescueAction>> rescueActionMap = new HashMap<>();
    private Map<Class<? extends Throwable>, EndAction> endActionMap = new HashMap<>();

    public FailureAction end() {
        rescueActionMap.put(currentExClass, currentActions);
        endActionMap.put(currentExClass, currentEndAction);
        FailureAction failureStrategy = new FailureAction();
        failureStrategy.setEndActionMap(endActionMap);
        failureStrategy.setRescueActionMap(rescueActionMap);
        return failureStrategy;
    }

    public ActionChain and() {
        rescueActionMap.put(currentExClass, currentActions);
        endActionMap.put(currentExClass, currentEndAction);
        currentActions = Lists.newArrayList();
        currentEndAction = null;
        return new ActionChain(this);
    }

    void addExceptionClass(Class<? extends Exception> exClass) {
        this.currentExClass = exClass;
    }

    void addAction(RescueAction action) {
        currentActions.add(action);
    }

    void setCurrentEndAction(EndAction currentEndAction) {
        this.currentEndAction = currentEndAction;
    }
}
